package org.arain.baseii.app.content;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.arain.baseii.app.navigation.NavigationService;
import org.arain.baseii.grid.AMGrid;
import org.arain.baseii.grid.ColumnsPojo;
import org.arain.baseii.grid.QueryParam;
import org.arain.baseii.utils.ArainResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("app/content")
public class ContentController {
	@Autowired
	private ContentService contentService;
	@Autowired
	private NavigationService navigationService;
	
	@RequestMapping("list_01")
	public String load_list(Model model) {
		List<ColumnsPojo> columns = new ArrayList<>();
		AMGrid grid = new AMGrid("id");
		grid.setUrl("/app/content/load_grid.do");
		grid.setToolbar("ContentToolbar");
		ColumnsPojo pojo;
		pojo = new ColumnsPojo("title", "标题");
		columns.add(pojo);
		pojo = new ColumnsPojo("type", "所属类型");
		columns.add(pojo);
		pojo = new ColumnsPojo("time", "发布时间");
		columns.add(pojo);
		pojo = new ColumnsPojo("recommend", "是否推荐");
		columns.add(pojo);
		pojo = new ColumnsPojo("enabled", "是否有效");
		columns.add(pojo);
		grid.setColumns(columns.toString());
		
		model.addAttribute("dataGrid", grid.toString());
		return "app/content/list_01";
	}
	
	@RequestMapping(value="load_grid.do",produces="text/json;charset=UTF-8")
	@ResponseBody
	public String load_grid(QueryParam param) {
		return contentService.load_grid(param);
	}
	
	@RequestMapping("add_01")
	public String add_01(ContentDto dto,HttpServletRequest request,Model model) {
		dto.setEnabled("1");
		model.addAttribute("ContentDto", dto);
		return "app/content/add_01";
	}
	
	@RequestMapping("edit_01")
	public String edit_01(ContentDto dto,HttpServletRequest request,Model model) {
		dto = contentService.load_01(dto);
		model.addAttribute("ContentDto", dto);
		return "app/content/add_01";
	}
	
	@RequestMapping(value="load_type",produces="text/json;charset=UTF-8")
	@ResponseBody
	public String load_type(HttpServletRequest request) {
		return navigationService.load_list(request);
	}
	
	@RequestMapping("save.do")
	@ResponseBody
	public ArainResult save_01(ContentDto dto) {
		return contentService.save_01(dto);
	}
}
